#include "db.hpp"
#define HOST "127.0.0.1"
#define PORT 3306
#define USER "root"
#define PASS "111111"
#define DBNAME "mqtt"
#include "libary/httplib.h"
#include "daemon.hpp"
using namespace std;

void Start(const httplib::Request& req, httplib::Response& resp)
{
	string body;
    file_util::read("wwwroot/index.html", body);
	resp.body += body;
	resp.set_header("Content-Type","text/html");
	resp.status=200;
}
int main()
{
    Daemon();
    user_table ut(HOST, USER, PASS);
	
    httplib::Server svr;    // 实例化Server类的对象用于搭建服务器
	svr.Get("/", Start);

    

    svr.Post("/display_by_id",[&](const httplib::Request&req, httplib::Response&resp){
        cout << "收到请求" << endl;
        Json::Value val;
        json_util::deserialize(req.body, val);
        string id = val["id"].asString();

        Json::Value root;
        ut.select_by_id(id, root);
        string body;
        json_util::serialize(root, body);
        resp.body.clear();
        resp.body += body;
        resp.set_header("Content-Type","application/json");
        resp.status=200;
    });

    svr.Post("/display_by_name",[&](const httplib::Request&req, httplib::Response&resp){
        Json::Value val;
        json_util::deserialize(req.body, val);
        string name = val["name"].asString();

        Json::Value root;
        ut.select_by_name(name, root);
        string body;
        json_util::serialize(root, body);
        resp.body.clear();
        resp.body += body;
        resp.set_header("Content-Type","application/json");
        resp.status=200;
    });

    svr.Post("/display_all_note",[&](const httplib::Request&req, httplib::Response&resp){
        cout << "/display_all_note" << endl;
        Json::Value root;
        ut.select_all_note(root);
        string body;
        json_util::serialize(root, body);

        resp.body.clear();
        resp.body += body;
        resp.set_header("Content-Type","application/json");
        resp.status=200;
    });
    svr.listen("0.0.0.0",8080);
}


